import 'package:album/components/TopAppBar/TopAppbar.dart';
import 'package:album/pages/IndexPage/IndexPage.dart';
import 'package:album/pages/OrderPage/OrderPage.dart';
import 'package:album/pages/PersonalPage/PersonalPage.dart';
import 'package:album/pages/StrategyPage/StrategyPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bmflocation/flutter_bmflocation.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:album/utils/provider.dart';
import 'components/BottomNavigationBar/BottomNavigationBar.dart';
import 'dart:io' show Platform;
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
    show BMFMapSDK, BMF_COORD_TYPE;

void main() {
  /**
   * 状态栏背景透明
   * 状态栏标题字体颜色
   */
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
        statusBarColor: Color(0x00FFFFFF), //状态栏背景颜色
        statusBarIconBrightness: Brightness.dark // dark:一般显示黑色   light：一般显示白色
    ),
  );
  runApp(ChangeNotifierProvider(
    create: (context) => GlobalProvider(),
    child: const MyAppWidget(),
  ));
}

class MyAppWidget extends StatefulWidget {
  const MyAppWidget({Key? key}) : super(key: key);

  @override
  createState() => MyAppState();
}

class MyAppState extends State<MyAppWidget> {
  List pageLists = [
    const IndexPage(),
    const StrategyPage(),
    const OrderPage(),
    const PersonalPage(),
  ];

  List appBarLists = [
    const IndexAppBar(),
    const StrategyAppBar(),
    const OrderAppBar(),
    const PersonalAppBar(),
  ];

  // 动态申请定位权限
  void requestPermission() async {
    // 申请权限
    bool hasLocationPermission = await requestLocationPermission();
    if (hasLocationPermission) {
      // 权限申请通过
    } else {}
  }

  /// 申请定位权限
  /// 授予定位权限返回true， 否则返回false
  Future<bool> requestLocationPermission() async {
    //获取当前的权限
    var status = await Permission.location.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.location.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    LocationFlutterPlugin myLocPlugin = LocationFlutterPlugin();

    /// 动态申请定位权限
    requestPermission();
    // 设置是否隐私政策
    myLocPlugin.setAgreePrivacy(true);
    BMFMapSDK.setAgreePrivacy(true);
    if (Platform.isIOS) {
      /// 设置ios端ak, android端ak可以直接在清单文件中配置
      myLocPlugin.authAK('请 输 入 您 的 AK');
      BMFMapSDK.setApiKeyAndCoordType('请 输 入 您 的 AK', BMF_COORD_TYPE.BD09LL);
    } else if (Platform.isAndroid) {
      // Android 目前不支持接口设置Apikey,
      // 请在主工程的Manifest文件里设置，详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
      BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
    }

    /// iOS端鉴权结果
    myLocPlugin.getApiKeyCallback(callback: (String result) {
      String str = result;
      print('鉴权结果：' + str);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Album',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.white,
      ),
      home: Scaffold(
        appBar: AppBar(
          elevation: 0, //消除阴影
          toolbarHeight: 50,
          titleSpacing: 0,
          backgroundColor: Colors.white,
          title:
          appBarLists[Provider.of<GlobalProvider>(context).getCurrentIndex],
        ),
        backgroundColor: Colors.white,
        body: pageLists[Provider.of<GlobalProvider>(context).getCurrentIndex],
        // body: HomePage(),
        bottomNavigationBar: const BottomNavigationWidget(),
      ),
    );
  }
}
